<template>
  <div>
    <!-- 三级分类 -->
    <CategoryVue :scene="scene" />
    <el-card style="margin: 10px 0px">
      <div v-show="scene == 1">
        <el-button type="primary" size="default" icon="Plus" @click="addAtt">
          添加属性
        </el-button>
        <el-table
          :border="false"
          stripe
          style="margin: 10px 0px"
          :data="attrArr"
        >
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="80px"
          ></el-table-column>
          <el-table-column
            label="属性名称"
            width="120px"
            prop="attrName"
          ></el-table-column>
          <el-table-column label="属性值名称">
            <template #="{ row }">
              <el-tag v-for="item in row.attrValueList" style="margin: 5px">
                {{ item.valueName }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120px">
            <template #="{ row }">
              <el-button
                type="primary"
                icon="Edit"
                @click="setAttr(row)"
                size="small"
              ></el-button>
              <el-button
                @click="deleteAttr(row)"
                type="primary"
                icon="Delete"
                size="small"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-show="scene == 0">
        <el-form :inline="true">
          <el-form-item label="属性名称">
            <el-input
              placeholder="请输入属性名称"
              v-model="attrParams.attrName"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-button
          :disabled="attrParams.attrName ? false : true"
          type="primary"
          size="default"
          icon="Plus"
          @click="addAttrValueList"
        >
          添加属性值
        </el-button>
        <el-button type="primary" size="default" @click="cancel">
          取消
        </el-button>
        <el-table
          :border="false"
          style="margin: 10px 0px"
          :data="attrParams.attrValueList"
        >
          <el-table-column
            label="序号"
            width="80px"
            type="index"
            align="center"
          ></el-table-column>
          <el-table-column label="属性值名称">
            <template #="{ row, $index }">
              <el-input
                placeholder="请输入属性值名称"
                v-model="row.valueName"
                v-if="row.flag"
                @blur="toLook(row, $index)"
                :ref="
                  (vc: any) => {
                    inputArr[$index] = vc
                  }
                "
              ></el-input>
              <div v-else @click="toEdit(row, $index)">{{ row.valueName }}</div>
            </template>
          </el-table-column>
          <el-table-column label="序号">
            <template #="{ row, $index }">
              <el-button
                type="primary"
                size="small"
                icon="Delete"
                @click="attrParams.attrValueList.splice($index, 1)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button type="primary" size="default" @click="save">确定</el-button>
        <el-button type="primary" size="default" @click="setscene">
          取消
        </el-button>
      </div>
    </el-card>
  </div>
</template>
<script setup lang="ts">
import { nextTick, onUnmounted, reactive, ref, watch } from 'vue'
import { AddUpdataAttr, DeleteAttr, reqAttr } from '@/api/product/attr/attr'
import type { AttValue, Attr, AttrResponseData } from '@/api/product/attr/type'
import { ElMessage } from 'element-plus'
import Category from '@/stores/category'
//显示那个节目
let scene = ref(1)
//收集新增 数据
let attrParams = reactive<Attr>({
  attrName: '', //属性名字
  attrValueList: [],
  categoryId: '', //三级分类id
  categoryLevel: 3, //三级分类
})

const cancel = () => {
  setscene()
  attrParams.attrName = ''
  attrParams.attrValueList = []
  attrParams.categoryId = ''
}
const addAtt = () => {
  attrParams.attrName = ''
  attrParams.attrValueList = []
  attrParams.categoryId = ''
  attrParams.id = ''
  if (CategoryStores.c3Id == '') {
    ElMessage({
      type: 'error',
      message: '没有选择数据',
    })
    return
  }
  setscene()
}
const setscene = () => {
  if (scene.value == 1) {
    scene.value = 0
  } else {
    scene.value = 1
  }
}
let CategoryStores = Category()
let attrArr = ref<Attr[]>([])
let inputArr = ref<any>([])
watch(
  () => CategoryStores.c3Id,
  () => {
    attrArr.value = []
    if (!CategoryStores.c3Id) return
    getAttr()
  },
)
const getAttr = async () => {
  const { c1Id, c2Id, c3Id } = CategoryStores
  let req: AttrResponseData = await reqAttr(c1Id, c2Id, c3Id)
  if (req.code == 200) {
    attrArr.value = req.data
  } else {
    ElMessage({
      type: 'error',
      message: '获取属性错误',
    })
  }
}
const setAttr = (row: any) => {
  console.log(row)
  Object.assign(attrParams, JSON.parse(JSON.stringify(row)))
  setscene()
}
//添加属性值
const addAttrValueList = () => {
  attrParams.attrValueList.push({
    valueName: '',
    flag: true,
  })
  attrParams.categoryId = CategoryStores.c3Id
  nextTick(() => {
    inputArr.value[attrParams.attrValueList.length - 1].focus()
  })
}
//提交
const save = async () => {
  await AddUpdataAttr(attrParams)
  if (attrParams.id == null) {
    ElMessage({
      type: 'info',
      message: '添加成功',
    })
  } else {
    ElMessage({
      type: 'info',
      message: '修改成功',
    })
  }
  setscene()
  getAttr()
}
//失去焦点的事件回调
const toLook = (row: AttValue, index: number) => {
  if (row.valueName.trim() == '') {
    attrParams.attrValueList.splice(index, 1)
    ElMessage({
      type: 'error',
      message: '属性不能为空',
    })
    return
  }
  let re = attrParams.attrValueList.find((liem) => {
    if (liem != row) {
      return liem.valueName === row.valueName
    }
  })
  if (re) {
    attrParams.attrValueList.splice(index, 1)
    ElMessage({
      type: 'error',
      message: '属性不可以重复',
    })
    return
  }
  row.flag = !row.flag
}
const toEdit = (row: AttValue, index: number) => {
  row.flag = !row.flag

  nextTick(() => {
    inputArr.value[index].focus()
  })
}
onUnmounted(() => {
  CategoryStores.c1Id = ''
  CategoryStores.c2Id = ''
  CategoryStores.c3Id = ''
})

const deleteAttr = async (row: any) => {
  let re = await DeleteAttr(row.id)
  if (re.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    getAttr()
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}
</script>
<style scoped lang="scss"></style>
